import numpy as np
import pandas as pd

# =====================================================
# 计算滇中各区县新型城镇化与生态环境耦合度C和协调度D
# =====================================================
# 新型城镇化，生态环境
cv_file = '滇中县域-新型城镇化-综合评价得分-统一时序.xlsx'
en_file = '滇中县域-生态环境-综合评价得分-统一时序.xlsx'
df_cv = pd.read_excel(cv_file, sheet_name='综合得分', index_col=0)
# print('-----------新型城镇化水平-原始数据-----------')
# print(df_cv)
df_en = pd.read_excel(en_file, sheet_name='综合得分', index_col=0)
# print('-----------生态环境水平-原始数据-----------')
# print(df_cv)
zb_file = 'data/年鉴统计数据/县域数据/县域指标分类体系.xlsx'
# 加载州市
df_county = pd.read_excel(zb_file, sheet_name='区县')
county_names = np.array(df_county.columns)
# print('-----------区县-----------')
# print(county_names)
df_C = pd.DataFrame()
df_D1 = pd.DataFrame()
df_D2 = pd.DataFrame()
df_D3 = pd.DataFrame()
for county_name in county_names:
    city_cv = df_cv[county_name]
    city_en = df_en[county_name]
    # 计算乘积
    product = city_cv * city_en
    # 计算均值
    mean = (city_cv + city_en) / 2
    # 计算均值的平方
    square_of_mean = mean * mean
    # 计算耦合度
    C = np.sqrt(product / square_of_mean)
    df_C = df_C.append(C)
    # 计算T1
    T1 = 1/2 * city_cv + 1/2 * city_en
    # 计算协调度D1
    D1 = np.sqrt(C * T1)
    df_D1 = df_D1.append(D1)
    # 计算T2
    T2 = 1 / 3 * city_cv + 2 / 3 * city_en
    # 计算协调度D2
    D2 = np.sqrt(C * T2)
    df_D2 = df_D2.append(D2)
    # 计算T3
    T3 = 2 / 3 * city_cv + 1 / 3 * city_en
    # 计算协调度D3
    D3 = np.sqrt(C * T3)
    df_D3 = df_D3.append(D3)
# 保存
df_C_T = pd.DataFrame(df_C.values.T, columns=df_C.index, index=df_C.columns)
df_C_T.to_excel('滇中县域-新型城镇化与生态环境-耦合度.xlsx')
print(df_C_T)
df_D1_T = pd.DataFrame(df_D1.values.T, columns=df_D1.index, index=df_D1.columns)
df_D1_T.to_excel('滇中县域-新型城镇化与生态环境-协调度a2b2.xlsx')
df_D2_T = pd.DataFrame(df_D2.values.T, columns=df_D2.index, index=df_D2.columns)
df_D2_T.to_excel('滇中县域-新型城镇化与生态环境-协调度a1b2.xlsx')
df_D3_T = pd.DataFrame(df_D3.values.T, columns=df_D3.index, index=df_D3.columns)
df_D3_T.to_excel('滇中县域-新型城镇化与生态环境-协调度a2b1.xlsx')
